﻿using AutoMapper;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.Shopfloor.DTO;
using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.IServices.IShopfloor;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.IServices.IEquipment;
using PowerHub.MES.Entities.Equipment;
using PowerHub.MES.DTO.Equipment.QueryDTO;
using PowerHub.MES.DTO.Equipment.DTO;
using PowerHub.MES.IRepository.IEquipment;

namespace PowerHub.MES.Services.Equipment
{

    /// <summary>
    /// 设备类别接口实现层
    /// </summary>
    public class Equipment_EquipmentClassifyServices : BaseServices<Equipment_EquipmentClassify>, IEquipment_EquipmentClassifyServices
    {
        private IEquipment_EquipmentClassifyRepository _iEquipment_EquipmentClassifyRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public Equipment_EquipmentClassifyServices(PowerHubMesContext powerHubMesContext, IEquipment_EquipmentClassifyRepository iEquipment_EquipmentClassifyRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iEquipment_EquipmentClassifyRepository = iEquipment_EquipmentClassifyRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取设备类别列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<Equipment_EquipmentClassify>>> GetEquipment_EquipmentClassifyPageListAsync(QueryEquipment_EquipmentClassifyDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _iEquipment_EquipmentClassifyRepository.GetEquipment_EquipmentClassifyPageListAsync(query);
        }

        /// <summary>
        /// 添加设备类别
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateEquipment_EquipmentClassifyAsync(Equipment_EquipmentClassifyDTO query)
        {
            Equipment_EquipmentClassify foundation = await GetEntityAsync(d => d.ClassifyName.Equals(query.ClassifyName) && d.IsDel == 0);
            if (foundation != null)
            {
                throw new Exception("你输入的设备类别名称已存在！");
            }
            //实体转换
            var Equipment_EquipmentClassify = _mapper.Map<Equipment_EquipmentClassify>(query);
            Equipment_EquipmentClassify.Id = Guid.NewGuid().ToString();
            Equipment_EquipmentClassify.ClassifyNO = string.IsNullOrEmpty(query.ClassifyNO) || query.ClassifyNO.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.ClassifyName) : query.ClassifyNO;
            Equipment_EquipmentClassify.Creator = _userServices.UserName;

            return await _iEquipment_EquipmentClassifyRepository.CreateEquipment_EquipmentClassifyAsync(Equipment_EquipmentClassify);
        }



        /// <summary>
        /// 批量删除设备类别
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionEquipment_EquipmentClassifyAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iEquipment_EquipmentClassifyRepository.DeletionEquipment_EquipmentClassifyAsync(ids);
        }


        /// <summary>
        /// 更新设备类别
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateEquipment_EquipmentClassifyAsync(Equipment_EquipmentClassifyDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取设备类别信息
            Equipment_EquipmentClassify Equipment_EquipmentClassify = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (Equipment_EquipmentClassify == null)
            {
                throw new Exception("更新设备类别信息发生了异常,请刷新页面后试试");
            }
            Equipment_EquipmentClassify.ClassifyName = query.ClassifyName;
            Equipment_EquipmentClassify.Updator = _userServices.UserName;


            return await _iEquipment_EquipmentClassifyRepository.UpdateEquipment_EquipmentClassifyAsync(Equipment_EquipmentClassify);

        }
    }
}
